<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31
 * Time: 21:37
 */
namespace app\admin\model;

use app\common\model\Base;

class Article extends Base
{
    protected $name = "article";
    protected $pk = "articleid";
    protected $insert = ['hide'=>1];
//    protected $autoWriteTimestamp = true;

    /**
     * 获取订单列表 含append用法
     * Power by Mikkle
     * QQ:776329498
     * @param array $map
     * @param string $order
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function getArticleList($map=[],$order = 'id desc',$limit=8,$page=1){
        $field = 'articleid,type,cateid,title,pic,keywords,detail';
        $map['hide'] = 1;
        $data = $this->where($map)->field($field)
            ->limit($limit)
            ->page($page)
            ->order($order)->select();
        $re = [];
        foreach($data as $order){
            $re[] = $order->append([''])->toArray();
        }
        return $re;
    }

    /**
     * 订单详情
     * Power by Mikkle
     * QQ:776329498
     * @param array $map
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getArticleInfo($map=[]){
        if (!$map) return false;
        $field = 'articleid,type,cateid,title,pic,keywords,detail';
        $data = $this->where($map)->field($field)->find();
        if (!$data) return false;
        //$data['cart_list']=$data->hasManyThrough('VGuidAll','OrdersAccess','order_no','my_guid','order_no')->select();
        //$data['cart_list']=$this->getOrderAccessList($data['order_no']);
        return $data;
    }

    public function aaa($data='')
    {
        try{
            $this->startTrans();
            $this->data($data)->isUpdate(false)->save();
            $new_order = $this->order_no;
            if (!is_numeric($new_order)) throw new \Exception("商品订单添加失败");
            $this->hasMany('OrdersAccess', 'order_no', 'order_no')->saveAll($data);
            $this->commit();
            return ['code' => '1001', 'msg' => '商品订单添加成功', 'data' => ['order_no' => $new_order, 'cart_list' => $data]];
        }catch (\Exception $e){
            $this->rollback();
            return ['code'=>'1008','msg'=>'商品订单添加失败','data'];
        }


    }



}